package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;

import org.bson.BSON;
import org.bson.BSONObject;

import service.DAO;
import service.Util;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;

/**
 * Servlet implementation class index
 */
@WebServlet("/index")
public class index extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public index() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * Mongo m = new Mongo();
		 * 
		 * DB db = m.getDB("mydb"); DBCollection coll =
		 * db.getCollection("testCollection"); BasicDBObject dbo= new
		 * BasicDBObject(); dbo.put("email", "omer.kara"); dbo.put("name",
		 * "Omer Kara"); dbo.put("password", "01123589"); coll.insert(dbo);
		 * PrintWriter out = response.getWriter(); out.write("<p>hello</p>");
		 * out.write("<p>kayıt sayısı:"+coll.count()+"</p>"); DBObject myDoc =
		 * coll.findOne(); out.write(myDoc.toString()); DBCursor cur =
		 * coll.find(); Gson gson= new Gson(); User user= new User();
		 * user.email="omer.kara@gmail.com"; user.name="";
		 * user.password="01123589"; gson.toJson(user); while(cur.hasNext()) {
		 * out.write("<p>"+cur.next().toString()+"</p>"); }
		 */
		//HttpSession session = request.getSession();
		
		User u = (User) request.getSession().getAttribute("user");
		
		System.out.println("2session Id: " + request.getSession().getId() + "<br><br>");
		System.out.println("2The SESSION TIMEOUT period is "
		        + request.getSession().getMaxInactiveInterval() + " seconds.<br><br>");

		//System.out.println("@@@@@"+((User)session.getAttribute("user")).getName());
		if (Util.isLogin(u)) {
			System.out.println("-------");
			request.setAttribute("u", u);
			getServletContext().getRequestDispatcher(response.encodeURL("/index.jsp")).forward(request, response); 

		} else {
			System.out.println("+++++++");
			response.sendRedirect(response.encodeURL("../Splash.jsp"));
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
